/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sree.tali.dao.homepage.impl;

import com.sree.tali.common.CommonUtils;
import com.sree.tali.hibernate.pojo.IpPojo;
import com.sree.tali.hibernate.pojo.UserPojo;
import com.sree.tali.pojo.AdminHomepojo;
import com.sree.tali.pojo.ArticleTypes;
import com.sree.tali.pojo.Role;
import com.sree.tali.pojo.RoleArticleMapping;
import com.sree.tali.pojo.RoleUserMapping;
import com.sree.tali.pojo.UploadImagePojo;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author sreenuv
 */
public class ShowArticleDaoImpl implements ShowArticleDao{
    @Override
   public  List<UploadImagePojo> showHomeArticle( Session paramSession)
    {
     List<UploadImagePojo> homeDisplayList = new ArrayList<UploadImagePojo>();
    try
    {
      String str = "from UploadImagePojo pojo where pojo.status='A' and  pojo.description is not null and pojo.description!=''  order by pojo.articleNumber desc" ;
      Query localQuery = paramSession.createQuery(str);
      localQuery.setMaxResults(15);
      homeDisplayList = localQuery.list();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }   
    return  homeDisplayList;
    }
       public UploadImagePojo editArticle(Session paramSession ,int id)
       {
           UploadImagePojo uploadImagePojo = null;
           try
           {
               uploadImagePojo =(UploadImagePojo) paramSession.load(UploadImagePojo.class, id);
           }
           catch(Exception ex)
           {
               ex.printStackTrace();
           }
           finally
           {
               
           }
           return uploadImagePojo;
       }

      @Override
   public void updateArticle (  Session paramSession , UploadImagePojo uploadimagepojo)
   {

      Transaction localTransaction=null;
      try{
      localTransaction = paramSession.beginTransaction();
      paramSession.update(uploadimagepojo);
      localTransaction.commit();
      }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
    }
   
    public List<UploadImagePojo> viewArticles(Session paramSession , UploadImagePojo uploadimagepojo)
    {
     List<UploadImagePojo> viewarticlelist = new ArrayList<UploadImagePojo>();
    try
    {
      String str = "from UploadImagePojo pojo where (pojo.articleType ='"+uploadimagepojo.getViewArticleType()+"' or pojo.articleType is null or pojo.articleType='') and pojo.status ='"+uploadimagepojo.getViewStatus()+"'  order by pojo.id desc" ;
      Query localQuery = paramSession.createQuery(str);
      viewarticlelist = localQuery.list();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }   
    return  viewarticlelist;
      
      }
   @Override
   public  List<UploadImagePojo> showInactiveArticle( Session paramSession)
    {
     List<UploadImagePojo> inactiveArtcleList = new ArrayList<UploadImagePojo>();
    try
    {
      String str = "from UploadImagePojo pojo  order by pojo.id desc" ;
      Query localQuery = paramSession.createQuery(str);
      localQuery.setMaxResults(15);
      inactiveArtcleList = localQuery.list();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }   
    return  inactiveArtcleList;
    }
    
    @Override
    public UploadImagePojo showArticle(  Session paramSession , String articleName)
    {
        List<UploadImagePojo> homeDisplayList = new ArrayList<UploadImagePojo>();
    try
    {
      UploadImagePojo uploadimagepojo =null;
      String str = "from UploadImagePojo pojo where upper(pojo.url) ='"+articleName.toUpperCase()+"'";
      Query localQuery = paramSession.createQuery(str);
      homeDisplayList = localQuery.list();
      if(homeDisplayList!=null&&homeDisplayList.size()!=0)
      {
          uploadimagepojo = homeDisplayList.get(0);
          Transaction tx = paramSession.beginTransaction();
          uploadimagepojo.setCount(uploadimagepojo.getCount()+1);
          paramSession.update(uploadimagepojo);
          tx.commit();
      }
      return uploadimagepojo;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }
        return  null;
    }
    
   public List<UploadImagePojo> showTitles(  Session paramSession, String articleType)
   { 
     List<UploadImagePojo> homeDisplayList = new ArrayList<UploadImagePojo>();
    try
    {
      String str = "from UploadImagePojo pojo  where upper(pojo.articleType) = '"+articleType.toUpperCase()+"' and pojo.status ='A' order by pojo.articleNumber" ;
      Query localQuery = paramSession.createQuery(str);
      homeDisplayList = localQuery.list();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }   
    return  homeDisplayList;
   }
   
    @Override
   public List<UploadImagePojo> showRelatedArticle(Session paramSession ,UploadImagePojo uploadhomeimage)
   {
        List<UploadImagePojo> relaredArticles = new ArrayList<UploadImagePojo>();
    try
    {
      String str = "from UploadImagePojo pojo  where upper(pojo.articleType) = '"+uploadhomeimage.getArticleType().toUpperCase()+"' and pojo.status='A' and  pojo.description is not null and pojo.description!='' order by pojo.id" ;
      Query localQuery = paramSession.createQuery(str);
      localQuery.setMaxResults(8);
      relaredArticles = localQuery.list();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }   
    return  relaredArticles;
   }
   @Override
   public List<UploadImagePojo> showRecentArticle(Session paramSession ,UploadImagePojo uploadhomeimage)
   {
      List<UploadImagePojo> recentArticle = new ArrayList<UploadImagePojo>();
    try
    {
      String str = "from UploadImagePojo pojo where pojo.status='A' and  pojo.description is not null and pojo.description!='' order by pojo.id desc" ;
      Query localQuery = paramSession.createQuery(str);
      localQuery.setMaxResults(8);
      recentArticle = localQuery.list();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }   
    return  recentArticle;
   }
   public List<UploadImagePojo> showMostViewedArticle(Session paramSession ,UploadImagePojo uploadhomeimage)
    {
     List<UploadImagePojo> mostViewedArticle = new ArrayList<UploadImagePojo>();
    try
    {
      String str = "from UploadImagePojo pojo order by pojo.count desc" ;
      Query localQuery = paramSession.createQuery(str);
      localQuery.setMaxResults(8);
      mostViewedArticle = localQuery.list();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }   
    return  mostViewedArticle;
    }
    public void createUser(Session paramSession ,UploadImagePojo uploadimagePojo)
    {
      CommonUtils commonutils = new CommonUtils();
      UserPojo userpojo = new UserPojo();
      Transaction localTransaction =null;
      try{
      userpojo = commonutils.getUserPojo(userpojo,uploadimagePojo);
      userpojo.setStatus("A");
      localTransaction = paramSession.beginTransaction();
      paramSession.save(userpojo);
      localTransaction.commit();
      }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
    }

    public void addIp(Session paramSession ,UploadImagePojo uploadimagePojo)
    {
      CommonUtils commonutils = new CommonUtils();
      IpPojo ippojo = new IpPojo();
       Transaction localTransaction=null;
      try{
      ippojo = commonutils.getIpPojo(ippojo,uploadimagePojo);
      ippojo.setStatus("A");
      localTransaction = paramSession.beginTransaction();
      paramSession.save(ippojo);
      localTransaction.commit();
    }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
    }

    @Override
    public List<UserPojo> showUsers(  Session paramSession)
    {
        List<UserPojo> userList = new ArrayList<UserPojo>();
    try
    {
      String str = "from UserPojo user where user.status='A'";
      Query localQuery = paramSession.createQuery(str);
      userList = localQuery.list();
      return userList;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }
        return  userList;
    }
     @Override
     public List<IpPojo> showIps(  Session paramSession )
    {
        List<IpPojo> iplist = new ArrayList<IpPojo>();
    try
    {
      String str = "from IpPojo ip where ip.status='A'";
      Query localQuery = paramSession.createQuery(str);
      iplist = localQuery.list();
      return iplist;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }
        return  iplist;
    }

    public void inactivateUser(Session paramSession ,int id)
    {
      UserPojo userpojo = new UserPojo();
       Transaction localTransaction=null;
      try{
      userpojo = (UserPojo)paramSession.load(UserPojo.class, id);
      userpojo.setStatus("I");
      localTransaction = paramSession.beginTransaction();
      paramSession.update(userpojo);
      localTransaction.commit();
       }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
    }
    public void inactivateIp(Session paramSession ,int id)
    {
      IpPojo ippojo = new IpPojo();
      Transaction localTransaction=null;
      try{
      ippojo = (IpPojo)paramSession.load(IpPojo.class, id);
      ippojo.setStatus("I");
      localTransaction = paramSession.beginTransaction();
      paramSession.update(ippojo);
      localTransaction.commit();
      }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
    }
   public void deleteAllIps(Session paramSession)
    {
      Transaction localTransaction=null;
      try{
      List<IpPojo>  ipslist = showAllIps(paramSession);
      localTransaction = paramSession.beginTransaction();
      for(IpPojo ippojo: ipslist)
      paramSession.delete(ippojo);
      localTransaction.commit();
       }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
    }
    public List<IpPojo> showAllIps(  Session paramSession )
    {
        List<IpPojo> iplist = new ArrayList<IpPojo>();
    try
    {
      String str = "from IpPojo";
      Query localQuery = paramSession.createQuery(str);
      iplist = localQuery.list();
      return iplist;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
        return  iplist;
    }
    public void inactivateArticle(Session paramSession ,int id)
    {
      UploadImagePojo uploadimagepojo = new UploadImagePojo();
      Transaction localTransaction=null;
      try{
      uploadimagepojo = (UploadImagePojo)paramSession.load(UploadImagePojo.class, id);
      uploadimagepojo.setStatus("I");
      localTransaction = paramSession.beginTransaction();
      paramSession.update(uploadimagepojo);
      localTransaction.commit();
      }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
        
    }
     public void activateArticle(Session paramSession ,int id , int articleNumber)
     {
      UploadImagePojo uploadimagepojo = new UploadImagePojo();
      Transaction localTransaction=null;
      try{
      uploadimagepojo = (UploadImagePojo)paramSession.load(UploadImagePojo.class, id);
      uploadimagepojo.setStatus("A");
      if(uploadimagepojo.getArticleNumber()==0)
      {
          uploadimagepojo.setArticleNumber(articleNumber*10);
      }
      localTransaction = paramSession.beginTransaction();
      paramSession.update(uploadimagepojo);
      localTransaction.commit();
      }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
        
      
     }
     
     public void removeArticle(Session paramSession ,int id)
     {
      UploadImagePojo uploadimagepojo = new UploadImagePojo();
      Transaction localTransaction=null;
      try{
      uploadimagepojo = (UploadImagePojo)paramSession.load(UploadImagePojo.class, id);
        localTransaction = paramSession.beginTransaction();
      paramSession.delete(uploadimagepojo);
      localTransaction.commit();
      }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
        
     }
     public void removeUser(Session paramSession ,int id)
     {
       UserPojo userpojo = new UserPojo();
       Transaction localTransaction=null;
      try{
      userpojo = (UserPojo)paramSession.load(UserPojo.class, id);
      localTransaction = paramSession.beginTransaction();
      paramSession.delete(userpojo);
      localTransaction.commit();
       }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
     }
     public void removeIp(Session paramSession ,int id)
     {
      IpPojo ippojo = new IpPojo();
      Transaction localTransaction=null;
      try{
      ippojo = (IpPojo)paramSession.load(IpPojo.class, id);
      localTransaction = paramSession.beginTransaction();
      paramSession.delete(ippojo);
      localTransaction.commit();
      }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      } 
     }
     @Override
     public void addArticleTypes(Session paramSession , ArticleTypes articletypes )
       {
           Transaction localTransaction=null;
           try{
                     localTransaction = paramSession.beginTransaction();
                     paramSession.save(articletypes);
                     localTransaction.commit();
           }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      } 
              
       }
     public List<ArticleTypes> viewArticleTypes(Session paramSession )
       {
     List<ArticleTypes> articleTypes = new ArrayList<ArticleTypes>();
     try
     {
      String str = "from ArticleTypes";
      Query localQuery = paramSession.createQuery(str);
      articleTypes = localQuery.list();
      return articleTypes;
     }
    catch (Exception localException)
     {
      localException.printStackTrace();
     }
     finally
      {
          paramSession.close();
      } 
          return articleTypes;
     }
     public void removeArticleTypes(Session paramSession , int id)
     {
      ArticleTypes articleTypes = new ArticleTypes();
      Transaction localTransaction=null;
      try{
      articleTypes = (ArticleTypes)paramSession.load(ArticleTypes.class, id);
      localTransaction = paramSession.beginTransaction();
      paramSession.delete(articleTypes);
      localTransaction.commit();
      }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
     }


     
     
     
     public void addRole(Session paramSession , Role role )
     {
          Transaction localTransaction=null;
           try{
                     localTransaction = paramSession.beginTransaction();
                     paramSession.save(role);
                     localTransaction.commit();
           }
      catch(Exception e) {  localTransaction.rollback();e.printStackTrace(); }
      finally {paramSession.close(); } 
     }
     public List<Role> viewRole(Session paramSession )
     {
         List<Role> roles = new ArrayList<Role>();
     try
     {
      String str = "from Role";
      Query localQuery = paramSession.createQuery(str);
      roles = localQuery.list();
      return roles;
     }
    catch (Exception localException)
     {
      localException.printStackTrace();
     }
     finally
      {
          paramSession.close();
      } 
          return roles;
     }
     public void removeRole(Session paramSession , int id)
     {
      Role role = new Role();
      Transaction localTransaction=null;
      try{
      role = (Role)paramSession.load(Role.class, id);
      localTransaction = paramSession.beginTransaction();
      paramSession.delete(role);
      localTransaction.commit();
      }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
     }
     public void addRoleUser(Session paramSession , RoleUserMapping roleusermapping )
     {
       Transaction localTransaction=null;
           try{
                     localTransaction = paramSession.beginTransaction();
                     paramSession.save(roleusermapping);
                     localTransaction.commit();
           }
      catch(Exception e) {  localTransaction.rollback();e.printStackTrace(); }
      finally {paramSession.close(); } 
     }
     public List<RoleUserMapping> viewRoleUser(Session paramSession )
     {
         List<RoleUserMapping> roleusers = new ArrayList<RoleUserMapping>();
     try
     {
      String str = "from RoleUserMapping";
      Query localQuery = paramSession.createQuery(str);
      roleusers = localQuery.list();
      return roleusers;
     }
    catch (Exception localException)
     {
      localException.printStackTrace();
     }
     finally
      {
          paramSession.close();
      } 
          return roleusers;
     }
     public void removeRoleUser(Session paramSession , int id)
     {
      RoleUserMapping roleusermapping = new RoleUserMapping();
      Transaction localTransaction=null;
      try{
      roleusermapping = (RoleUserMapping)paramSession.load(RoleUserMapping.class, id);
      localTransaction = paramSession.beginTransaction();
      paramSession.delete(roleusermapping);
      localTransaction.commit();
      }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
     }
     public void addRoleArticle(Session paramSession , RoleArticleMapping rolearticlemapping )
     {
          Transaction localTransaction=null;
           try{
                     localTransaction = paramSession.beginTransaction();
                     paramSession.save(rolearticlemapping);
                     localTransaction.commit();
           }
      catch(Exception e) {  localTransaction.rollback();e.printStackTrace(); }
      finally {paramSession.close(); } 
     }
     public List<RoleArticleMapping> viewRoleArticle(Session paramSession )
     {
     List<RoleArticleMapping> rolearticle = new ArrayList<RoleArticleMapping>();
     try
     {
      String str = "from RoleArticleMapping";
      Query localQuery = paramSession.createQuery(str);
      rolearticle = localQuery.list();
      return rolearticle;
     }
    catch (Exception localException)
     {
      localException.printStackTrace();
     }
     finally
      {
          paramSession.close();
      } 
          return rolearticle;
     }
     public void removeRoleArticle(Session paramSession , int id)
     {
      RoleArticleMapping rolearticlemapping = new RoleArticleMapping();
      Transaction localTransaction=null;
      try{
      rolearticlemapping = (RoleArticleMapping)paramSession.load(RoleArticleMapping.class, id);
      localTransaction = paramSession.beginTransaction();
      paramSession.delete(rolearticlemapping);
      localTransaction.commit();
      }
      catch(Exception e)
      {
          localTransaction.rollback();
          e.printStackTrace();
      }
      finally
      {
          paramSession.close();
      }
     }

    
}
